var Login = function () {


    var basePath = (function () {
        var url = window.location + "";
        var h = url.split("//");
        var x = h[1].split("/");
        return h[0] + "//" + window.location.host + "/" + x[1] + "/";
    })();

    function login() {
        var $loginForm = $('.login-form');
        if ($loginForm.validate().form()) {
            $.get(basePath + "login/isLogin",{loginName:$("#username").val()},function(data) {
                if(data) {
                    toast.error("您的账户已在另一地点登陆");
                    if(confirm("该用户已登录，是否继续？")){
                        var $password = $("#password");
                        $password.val($password.val().md5());
                        $loginForm.submit();
                    }
                } else {
                    var $password = $("#password");
                    $password.val($password.val().md5());
                    $loginForm.submit();
                }
            });
        }
    }

    var handleLogin = function() {
        $('.login-form').validate({
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                }
            },

            messages: {
                username: {
                    required: i18n['usernameRequired']
                },
                password: {
                    required: i18n['passwordRequired']
                }
            },

            invalidHandler: function (event, validator) { //display error alert on form submit
                $('.alert-danger', $('.login-form')).show();
            },

            success: function (label) {
                label.closest('.form-group').removeClass('has-error');
                label.remove();
            },

            errorPlacement: function (error, element) {
                error.insertAfter(element.closest('.input-icon'));
            },

            submitHandler: function (form) {
                form.submit();
            }
        });

        $('#username').keypress(function (e) {
            if (e.which == 13) {
                $("#password").focus();
                return false;
            }
        }).focus();

        $('#password').keypress(function (e) {
            if (e.which == 13) {
                login();
                return false;
            }
        });

        $("#loginBtn").on("click",function(){
            login();
        });

        $.backstretch([
            "img/login/1.jpg",
            "img/login/2.jpg",
            "img/login/3.jpg",
            "img/login/4.jpg"
        ], {
            fade: 1000,
            duration: 8000
        });
    };

    return {
        //main function to initiate the module
        init: function () {
            handleLogin();
        }

    };

}();